name: build production images
on:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  backend-build:
    name: Build backend
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repo
      uses: actions/checkout@v3
    - name: Get versions
      id: getversion
      run: |
        export SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`
        export VERSION=$(cat ./backend/version)
        echo "version=$(echo $VERSION)" >> $GITHUB_OUTPUT
        echo Image tag versions will be latest and:
        echo version: $VERSION
    - name: Dockerhub Login
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_LOGIN }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - name: Set up Docker Buildx
      id: buildx
      uses: docker/setup-buildx-action@v3
    - name: Build and push
      id: docker_build
      uses: docker/build-push-action@v3
      with:
        context: ./backend
        file: ./backend/Dockerfile
        push: true
        tags: zeroglatte/rim-backend:latest, zeroglatte/rim-backend:${{ steps.getversion.outputs.version }}
  frontend-build:
    name: Build frontend
    runs-on: ubuntu-latest
    steps:
    - name: Checkout repo
      uses: actions/checkout@v3
    - name: Get versions
      id: getversion
      run: |
        export SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`
        export VERSION=$(cat ./frontend/version)-$SHORT_SHA
        echo "version=$(echo $VERSION)" >> $GITHUB_OUTPUT
        echo Image tag versions will be latest and:
        echo version: $VERSION
    - name: Dockerhub Login
      uses: docker/login-action@v3
      with:
        username: ${{ secrets.DOCKERHUB_LOGIN }}
        password: ${{ secrets.DOCKERHUB_TOKEN }}
    - name: Set up Docker Buildx
      id: buildx
      uses: docker/setup-buildx-action@v3
    - name: Build and push
      id: docker_build
      uses: docker/build-push-action@v3
      with:
        context: ./frontend
        file: ./frontend/Dockerfile
        push: true
        tags: zeroglatte/rim-frontend:latest, zeroglatte/rim-frontend:${{ steps.getversion.outputs.version }}